package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.CameraDO;
import com.bootdo.system.service.CameraService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 *
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-07-17 17:36:16
 */

@Controller
@RequestMapping("/sys/camera")
public class CameraController {
	@Autowired
	private CameraService cameraService;

	@GetMapping()
	@RequiresPermissions("sys:camera:camera")
	String Camera(){
	    return "system/camera/camera";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:camera:camera")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<CameraDO> cameraList = cameraService.list(query);
		int total = cameraService.count(query);
		PageUtils pageUtils = new PageUtils(cameraList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("sys:camera:add")
	String add(){
	    return "system/camera/add";
	}

	@GetMapping("/edit/{cameraId}")
	@RequiresPermissions("sys:camera:edit")
	String edit(@PathVariable("cameraId") Integer cameraId,Model model){
		CameraDO camera = cameraService.get(cameraId);
		model.addAttribute("camera", camera);
	    return "system/camera/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:camera:add")
	public R save( CameraDO camera){
		if(cameraService.save(camera)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:camera:edit")
	public R update( CameraDO camera){
		cameraService.update(camera);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:camera:remove")
	public R remove( Integer cameraId){
		if(cameraService.remove(cameraId)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:camera:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] cameraIds){
		cameraService.batchRemove(cameraIds);
		return R.ok();
	}

}
